package http

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"go_private_im/internal/logic/biz"
	"io/ioutil"
)

func (s *Server) pushKeys(c *gin.Context) {

	arg := biz.KeysArg{}
	if err := c.BindQuery(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	// read message
	msg, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	arg.Msg = msg
	if err = s.logic.PushKeys(context.TODO(),&arg); err != nil {
		result(c, nil, RequestErr)
		return
	}
	result(c, nil, OK)
}

func (s *Server) pushUsers(c *gin.Context) {
	var arg = biz.UserArg{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	arg.Token = c.Request.Header.Get("token")
	if err := s.logic.PushUserIds(context.TODO(), &arg); err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, nil, OK)
}

func (s *Server) pushRoom(c *gin.Context) {
	var arg = biz.RoomArg{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	arg.Token = c.Request.Header.Get("token")


	if err := s.logic.PushRoom(c, &arg); err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, nil, OK)
}

func (s *Server) AddRoomDetails (c *gin.Context)  {
	var arg = biz.AddRoomDetailsArg{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	arg.Token = c.Request.Header.Get("token")
	fmt.Println(arg,"dadadadad")
	if err := s.logic.AddRoomUsers(c, &arg); err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, nil, OK)
	fmt.Println(arg)
}
func (s *Server) InitiativeRoomUser (c *gin.Context)  {
	var arg = biz.InitiativeRoomUserArg{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	arg.Token = c.Request.Header.Get("token")
	if err := s.logic.InitiativeRoomUser(c, &arg); err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, nil, OK)
	fmt.Println(arg)
}


//
func (s *Server) pushAll(c *gin.Context) {
	var arg = biz.PushAllArg{}
	if err := c.BindQuery(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	msg, err := ioutil.ReadAll(c.Request.Body)
	arg.Msg = msg
	if err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	if err = s.logic.PushAll(c, &arg); err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, nil, OK)
}


//退出群聊
func (s *Server) ActiveOutRoom(c *gin.Context)  {
	//退出群聊
	var arg = biz.OutRoom{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	arg.Token = c.Request.Header.Get("token")
	if err := s.logic.ActiveOutRoom(c,&arg); err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, nil, OK)
}

func (s *Server) kickUserOutRoom(c *gin.Context)  {
	var arg = biz.KickUserOutRoom{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	arg.Token = c.Request.Header.Get("token")
	if err := s.logic.KickUserOutRoom(c,&arg); err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, nil, OK)
}

func (s *Server) GetRooms(c *gin.Context)  {
	//退出群聊
	var arg = biz.RoomlistArg{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	arg.Token = c.Request.Header.Get("token")
	list,err := s.logic.GetRooms(c,&arg)
	if  err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, list, OK)
}

func (s *Server) GetRoomMsg(c *gin.Context)  {
	//退出群聊
	var arg = biz.RoomMsgListArg{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	arg.Token = c.Request.Header.Get("token")
	list,err := s.logic.GetRoomMsg(c,&arg)
	if  err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, list, OK)
}
func (s *Server) Login(c *gin.Context)  {
	//退出群聊
	var arg = biz.LoginArg{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	token,err := s.logic.Login(c,&arg)
	if  err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, token, OK)
}

func (s *Server) LoginOut(c *gin.Context)  {
	//退出群聊
	var arg = biz.LoginArg{}
	if err := c.ShouldBind(&arg); err != nil {
		errors(c, RequestErr, err.Error())
		return
	}
	err := s.logic.LoginOut(c,&arg)
	if  err != nil {
		errors(c, ServerErr, err.Error())
		return
	}
	result(c, nil, OK)
}